package com.jiamao.goplay.view;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import me.goldze.mvvmhabit.utils.KLog;

public class DouYinLayoutManager extends LinearLayoutManager implements RecyclerView.OnChildAttachStateChangeListener{
    //判断是否上滑还是下滑
    private int mDrift;

    //吸顶，吸底
    private PagerSnapHelper pagerSnapHelper;




    public DouYinLayoutManager(Context context) {
        super(context);
    }

    public DouYinLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        pagerSnapHelper = new PagerSnapHelper();

    }

    /**
     * 当manager完全添加到recycleview中是会被调用
     * @param view
     */
    @Override
    public void onAttachedToWindow(RecyclerView view) {
        view.addOnChildAttachStateChangeListener(this);
        pagerSnapHelper.attachToRecyclerView(view);
        super.onAttachedToWindow(view);
    }

    @Override
    public boolean canScrollVertically() {
        return super.canScrollVertically();
    }

    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        mDrift = dy;
        return super.scrollVerticallyBy(dy, recycler, state);

    }

    @Override
    public void onChildViewAttachedToWindow(@NonNull View view) {
        KLog.d("onChildViewAttachedToWindow :"+mDrift);
        if (onViewPagerListener != null){
            onViewPagerListener.onPageSelected(false,view);
        }
        if (mDrift > 0){

//            //向上滑
//            if (onViewPagerListener != null && Math.abs(mDrift) == view.getHeight()){
//                onViewPagerListener.onPageSelected(false,view);
//            }
        }else {
//            //向下滑
//            if (onViewPagerListener != null && Math.abs(mDrift) == view.getHeight()){
//                onViewPagerListener.onPageSelected(true,view);
//            }
        }
    }

    @Override
    public void onChildViewDetachedFromWindow(@NonNull View view) {
        KLog.d("onChildViewDetachedFromWindow :"+mDrift);
        if (mDrift >= 0){
            KLog.d("addlookeduser :");

            //向上滑
//            if (onViewPagerListener != null){
//                onViewPagerListener.onPageRelease(true,view);
//            }
        }else {
            //向下滑
//            if (onViewPagerListener != null){
//                onViewPagerListener.onPageRelease(false,view);
//            }
        }
    }

    onViewPagerListener onViewPagerListener;
    @Override
    public void onScrollStateChanged(int state) {
        KLog.d("addlookeduser :"+state);
        switch (state){
            case RecyclerView.SCROLL_STATE_IDLE:
                //当前显示的item
                View snapView = pagerSnapHelper.findSnapView(this);
                if (onViewPagerListener != null){
                 //   onViewPagerListener.onPageSelected(false,snapView);
                }
                break;
        }
        super.onScrollStateChanged(state);

    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        super.smoothScrollToPosition(recyclerView, state, position);
        KLog.d("smoothScrollToPosition :"+state+" position:"+position);
        if (onViewPagerListener != null){
            onViewPagerListener.smoothScrollToPosition(position);
        }
    }

    public void setonViewPagerListener(onViewPagerListener lsn){
        this.onViewPagerListener = lsn;
    }
    public interface onViewPagerListener{
        void onPageSelected(boolean isfa,View spanVie);
        void smoothScrollToPosition(int position);
    }}
